package com.shyroke.service.serviceImpl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.swing.plaf.basic.BasicScrollPaneUI.VSBChangeListener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.shyroke.entity.XiaoShuo;
import com.shyroke.entity.XiaoShuoBean;
import com.shyroke.entity.XiaoShuoEnum;
import com.shyroke.mapper.XiaoShuoMapper;
import com.shyroke.service.XiaoShuoServiceI;

@Service
public class XiaoShuoServiceImpl implements XiaoShuoServiceI {

	@Autowired
	private XiaoShuoMapper xiaoShuoMapper;

	@Override
	public List<String> getAllXiaoshuoByUserId(Integer userId, ServletContext context, String quanxian) {
		String quanxian1 = null;
		if (XiaoShuoEnum.SHOW_ALL.equals(quanxian)) {
			quanxian1 = null;
		} else if (XiaoShuoEnum.SHOW_PRIVATE.equals(quanxian)) {
			quanxian1 = "私密";
		} else if (XiaoShuoEnum.SHOW_PUBLIC.equals(quanxian)) {
			quanxian1 = "公开";
		}

		List<String> xiaoshuoNameList = xiaoShuoMapper.selectByIdAndQuanxian(userId, quanxian1);

		return xiaoshuoNameList;
	}

	@Override
	public void insert(XiaoShuo xiaoShuo,MultipartFile file) throws UnsupportedEncodingException, IOException {
		InputStreamReader isr = new InputStreamReader(file.getInputStream(),"UTF-8");
		BufferedReader br = new BufferedReader(isr);
		StringBuffer stringBuffer= new StringBuffer();
		String str;
		int i =1;
		while ((str = br.readLine()) != null) {
			
			if(str!=null) {
				str="<p id='"+i+"'>"+str+"</p>";
				stringBuffer.append(str); 
				i=i+1;
			}
			
		}
		xiaoShuo.setContent(stringBuffer.toString());
		xiaoShuoMapper.insert(xiaoShuo);

	}

	@Override
	public XiaoShuo selectByUserIdAndFileName(Integer userId, String wenjianjia) {
	
		XiaoShuo xiaoshuo = null;
		
		xiaoshuo = xiaoShuoMapper.selectByIdAndFileName(userId,wenjianjia);
		
		return xiaoshuo;
	}

}
